package moonlander;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Polygon;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import javax.swing.JLabel;
import javax.swing.JOptionPane;




public class LanderCanvas extends Canvas implements KeyListener{

	private Canvas canvas;
	private int[] groundXS = {0 ,30 ,40 ,100,140,160,180,200,220,230,300,310,330,350,
			360,400,410,435,460,465,500,545,560,575,580,600,600,0};
	private int[] groundYS = {500,450,480,510,350,400,395,480,490,480,480,520,515,520,
			515,550,400,350,360,400,410,480,455,465,480,500,600,600};
	private int[] landerXS = {11,13,27,29,30,26,37,40,40,30,30,33,24,
			21,24,16,19, 16, 7, 0, 0,10,10, 3,14,10};
	private int[] landerYS = { 5, 0,0, 5, 20,20,35,35,40,40,35,35,20,
	20,25,25,20,20,35,35,40,40,35,35,20,20};
	
	
	
	private static int x;
	private static int y;
	private Polygon poly;
	private Timer timer;
	private BufferStrategy buffer;
	 static boolean boost = false;
	 public static Rectangle lander;
	 private final Rectangle landingPad = new Rectangle (230,480,70,10);
	 MoonKeyListener moon = new MoonKeyListener();
	 


	public LanderCanvas(){
		this.setPreferredSize(new Dimension (600,600));
		x=0;
		y=0;
		timer = new Timer();
		timer.start();
		poly = new Polygon();
		for(int i =0; i<groundXS.length; i++){
			poly.addPoint(groundXS[i], groundYS[i]);
		}		
	    }
	
	public static int getYPos(){
		return y;
	}
	public static void setYPos(int a){
		y=a;
	}
	public static int getXPos(){
		return x;
	}
	public static void setXPos(int a){
		x=a;
	}
	
	public void paint(Graphics g) {

		g.setColor(Color.BLACK);
		g.fillRect(0,0,getWidth(),getHeight());
		
//		g.setColor(Color.red);
//		g.fillRect(Rect.x, Rect.y, Rect.width, Rect.height);
	
		g.setColor(Color.gray);
		g.fillPolygon(groundXS, groundYS, 28);
		
		g.setColor(Color.red);
		g.fillRect(landingPad.x, landingPad.y, landingPad.width, landingPad.height);

		if(poly.intersects(x,y,40,40)){
			timer.stop();
			g.translate(x,y);
			lander = new Rectangle (x,y,40,40);
			g.setColor(Color.lightGray);
			g.fillPolygon(landerXS , landerYS, 26);
			collision(x,y);
			}
		if(landingPad.intersects(x,y,40,40)){
			timer.stop();
			g.translate(x,y);
			lander = new Rectangle (x,y,40,40);
			g.setColor(Color.lightGray);
			g.fillPolygon(landerXS , landerYS, 26);
			collision(x,y);
			}
		
		g.translate(x,y);
		lander = new Rectangle (x,y,40,40);
		g.setColor(Color.lightGray);
		g.fillPolygon(landerXS , landerYS, 26);
	
		if (boost){
			g.setColor(Color.orange);
			g.fillRect(16,24,10,10);
			boost=false;
		}
		
		

		
		createBufferStrategy(2);

		
		}
	
	private void collision(int x, int y) {
		
		int r=0;
		if(landingPad.intersects(lander)){
			timer.stop();
			JOptionPane.showMessageDialog(null, "You Have Won! Congratulations!");
			r = JOptionPane.showConfirmDialog(this, new JLabel("Play Again?"),
					"Warning!", JOptionPane.YES_NO_CANCEL_OPTION,
					JOptionPane.WARNING_MESSAGE);
			if (r==0){repaint();
			setVisible(false);
			new LanderFrame();
			moon.setFuel(100);
			timer.resume();}
		}
		if (!landingPad.intersects(lander)){
			r = JOptionPane.showConfirmDialog(this, new JLabel("You Have Crashed. Play Again?"),
				"Warning!", JOptionPane.YES_NO_CANCEL_OPTION,
				JOptionPane.WARNING_MESSAGE);
		if (r==0){repaint();
		setVisible(false);
		new LanderFrame();
		moon.setFuel(100);
		timer.resume();}
		}
		if (r!=0)System.exit(1);
		
		
	}

	public class Timer extends Thread {
		
		public void run() {
		while(1==1) {
		try {
			
		Thread.sleep(100); 
		int y = LanderCanvas.getYPos();
		int gravity = y;
		if (gravity <=10){gravity+=2;}
		if (gravity <=20){gravity+=4;}
		else gravity = (int) (y *1.05);

		LanderCanvas.setYPos(gravity);

		
		
		repaint();

		} catch(InterruptedException e) {}
		} } }

	@Override
	public void keyPressed(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyTyped(KeyEvent e) {}


}
